#include "surroundingsWidget.h"
#include <QDebug>
#include <QPushButton>
#include <QHeaderView>
surroundingsWidget::surroundingsWidget(QWidget *parent) : QWidget(parent)
{
    setWindowTitle("查看周边");
    setFixedSize(600,600);
    surMessage->setFixedSize(600,600);
    connect(surMessage, &QTableWidget::itemClicked, [=](QTableWidgetItem *item){
        if(item->column() == 3)
        {
            emit SendSrdNum(item->row());
            this->close();
        }
    });
}

/**
 * @brief 获取主程序发来的pkg指针
 * @param pkg
 */
void surroundingsWidget::GetPkg(package *pkg)
{
    this->pkg = pkg;
    f(pkg);
}

/**
 * @brief 实际应当运行的窗口主程序
 * @param pkg
 */
void surroundingsWidget::f(package *pkg)
{
    bool srdDebug = true;
    int debugNum = 0;
    if(srdDebug)
    {
        qDebug() << pkg->myClock.time << "---------------surroundingsWidget run---------------";
    }
    QList<QString> headerText;
    QString addName;
    QString addDistance;
    QString addTime;
    QString startGuiding;
    pkg->state.navigationRuningFlag = false;
    if(pkg->state.suspendFlag == 0)
        pkg->myClock.speed = 1;

    headerText << "名称" << "距离" << "耗时" << "点击即可开始导航";
    surMessage->setHorizontalHeaderLabels(headerText);
    surMessage->verticalHeader()->setVisible(false);
    surMessage->setGeometry(50,50,510,500);
    surMessage->setEditTriggers(QAbstractItemView::NoEditTriggers);
    lineNum = (pkg->outP.matchingLocation.length() < 10) ? pkg->outP.matchingLocation.length() : 10;
    if(srdDebug)
    {
        qDebug() << ++debugNum << "lineNum:" << lineNum;
    }
    for(int i = 0; i < lineNum; i++)
    {
        if(srdDebug)
        {
            qDebug() << ++debugNum << "i:" << i;
        }
        if(i < lineNum - 1 && surMessage->rowCount() < 10)
            surMessage->insertRow(i);
        for(int j = 0; j < columnNum; j++)
        {
            QTableWidgetItem *item = new QTableWidgetItem;
             if(j == 0)
             {
                 if(srdDebug)
                 {
                     qDebug() << ++debugNum << "matchingLocation:" << pkg->outP.matchingLocation[i];
                 }
                addName = pkg->outP.matchingLocation[i];
                item->setText(addName);
                item->setTextAlignment(0x0084);
                surMessage->setItem(i,j,item);
             }
             else if(j == 1)
             {
                 if(srdDebug)
                 {
                     qDebug() << ++debugNum << "matchingDistance:" << pkg->outP.matchingDistance[i];
                 }
                 addDistance = QString::number(pkg->outP.matchingDistance[i]);
                 item->setText(addDistance);
                 item->setTextAlignment(0x0084);
                 surMessage->setItem(i,j,item);
             }
             else if(j == 2)
             {
                 if(srdDebug)
                 {
                     qDebug() << ++debugNum << "matchingTimeSpent:" << pkg->outP.matchingTimeSpent[i];
                 }
                 addTime = QString::number(pkg->outP.matchingTimeSpent[i]);
                 item->setText(addTime);
                 item->setTextAlignment(0x0084);
                 surMessage->setItem(i,j,item);
             }
             else if(j == 3)
             {
                 startGuiding = "开始导航";
                 item->setText(startGuiding);
                 item->setTextAlignment(0x0084);
                 surMessage->setItem(i,j,item);
             }
       }
    }
    if(srdDebug)
    {
        qDebug() << pkg->myClock.time << "---------------surroundingsWidget end---------------";
    }
}

/**
 * @brief 关闭窗口前做的事：
 * 将时间倍率调回用户设置的倍率
 * @param event
 */
void surroundingsWidget::closeEvent(QCloseEvent *)
{
    surMessage->clear();
    pkg->state.navigationRuningFlag = true;
    if(pkg->state.suspendFlag == 0)
        pkg->myClock.speed = pkg->myClock.userSpeed;
}
